package com.javajavadog.ui;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.javajavadog.ui.activitypatterns.PatternListActivity;
import com.javajavadog.ui.base.FragmentHostActivity;
import com.javajavadog.ui.elevation.ElevationMenuActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends FragmentHostActivity {
    @Override
    public String getTitleText() {
        return "UI Samples";
    }

    @Override
    public Fragment getContentFragment() {
        return MainFragment.newInstance();
    }

    @Override
    public boolean enableBackButton() {
        return false;
    }

    public static class MainFragment extends Fragment {
        public int mListViewId;

        public static MainFragment newInstance() {
            MainFragment f = new MainFragment();
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            mListViewId = View.generateViewId();
            ListView lv = new ListView(getActivity());
            addFooter(lv);
            lv.setId(mListViewId);
            return lv;
        }

        private void addFooter(ListView lv) {
            lv.setFooterDividersEnabled(false);
            View space = LayoutInflater.from(getActivity()).inflate(R.layout.space, lv, false);
            lv.addFooterView(space, null, false);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            ListView lv = (ListView) view.findViewById(mListViewId);
            lv.setAdapter(createListData());
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    TextView t = (TextView) view.findViewById(R.id.text_view_list_item);

                    if (null == t) {
                        return;
                    }

                    String item = t.getText().toString();
                    Intent intent;
                    switch (item) {
                        case "Basic controls":
                            intent = new Intent(getActivity(), ControlsBasicActivity.class);
                            startActivity(intent);
                            break;
                        case "Advanced controls":
                            break;
                        case "Tabs":
                            intent = new Intent(getActivity(), TabsActivity.class);
                            startActivity(intent);
                            break;
                        case "Activity patterns":
                            intent = new Intent(getActivity(), PatternListActivity.class);
                            startActivity(intent);
                            break;
                        case "Elevation":
                            intent = new Intent(getActivity(), ElevationMenuActivity.class);
                            startActivity(intent);
                            break;
                    }
                }
            });
        }

        private ListAdapter createListData() {
            List<Map<String, String>> data = new ArrayList<>();

            Map<String, String> item01 = new HashMap<>();
            item01.put("item_name", "Basic controls");
            item01.put("item_subtitle", "Demo basic controls provided by Android");
            Map<String, String> item02 = new HashMap<>();
            item02.put("item_name", "Advanced controls");
            item02.put("item_subtitle", "Demo advanced custom controls");
            Map<String, String> item03 = new HashMap<>();
            item03.put("item_name", "Drawables");
            item03.put("item_subtitle", "Various drawables");
            Map<String, String> item04 = new HashMap<>();
            item04.put("item_name", "Activity patterns");
            item04.put("item_subtitle", "Highly customized activities handle common cases");
            Map<String, String> item05 = new HashMap<>();
            item05.put("item_name", "Layouts");
            item05.put("item_subtitle", "Various layouts");
            Map<String, String> item06 = new HashMap<>();
            item06.put("item_name", "View animation");
            item06.put("item_subtitle", "Various view animation to enhance user experience");
            Map<String, String> item07 = new HashMap<>();
            item07.put("item_name", "Event transformation");
            item07.put("item_subtitle", "Dynamic views follow events");
            Map<String, String> item08 = new HashMap<>();
            item08.put("item_name", "Multi thread UI");
            item08.put("item_subtitle", "Smooth experience when processing data");
            Map<String, String> item09 = new HashMap<>();
            item09.put("item_name", "Color swift");
            item09.put("item_subtitle", "Shows a color swift demo");
            Map<String, String> item10 = new HashMap<>();
            item10.put("item_name", "Tabs");
            item10.put("item_subtitle", "Tabs layout often as application's main entry");
            Map<String, String> item11 = new HashMap<>();
            item11.put("item_name", "Elevation");
            item11.put("item_subtitle", "Material design elevation demo");

            data.add(item01);
            data.add(item02);
            data.add(item03);
            data.add(item04);
            data.add(item05);
            data.add(item06);
            data.add(item07);
            data.add(item08);
            data.add(item09);
            data.add(item10);
            data.add(item11);

            ListAdapter adapter = new SimpleAdapter(getActivity(), data,
                    R.layout.list_item_simple, new String[]{"item_name", "item_subtitle"},
                    new int[]{R.id.text_view_list_item, R.id.text_view_list_item_subtitle});

            return adapter;
        }
    }
}
